package com.ll.agent;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

/**
 * @description:
 * @author: LiuLei
 * @create: 2024-07-18
 **/
public class JpAgentCore {
    // 提前
    public static void premain(String args, Instrumentation instrumentation){
        System.out.println("two 参数");
        instrumentation.addTransformer(new JpClassFileTransformerDemo());
    }

    // 运行中
    public static void agentmain (String agentArgs, Instrumentation inst) throws UnmodifiableClassException {
        System.out.println("agentmain called");
        // 增量的
        inst.addTransformer(new JpClassFileTransformerDemo(), true);
        // 存量的
        Class classes[] = inst.getAllLoadedClasses();
        for (int i = 0; i < classes.length; i++) {
            if (classes[i].getName().equals("MyTestMain")) {
                System.out.println("Reloading: " + classes[i].getName());
                inst.retransformClasses(classes[i]);
                break;
            }
        }
    }
}